package com.ddpaysdk.wxapi;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WxpayModule extends ReactContextBaseJavaModule {
    static IWXAPI api = null;
    static String WXAppid = "";
    static Promise promise = null;
    Context rContext;
    public IWXAPI getWXApi() {
        if (api == null) {
            return WXAPIFactory.createWXAPI(rContext, getAPPID());
        }
        return api;

    }
    public String getAPPID() {
        ApplicationInfo mApplicationInfo = null;
        try {
            //获取 AndroidManifest.xml 中 application 标签内插入的 <meta-data android:name="UMENG_CHANNEL" android:value="umeng" />
            //通过 UMENG_CHANNEL 获取到value：umeng
            mApplicationInfo = getReactApplicationContext().getPackageManager().getApplicationInfo(getReactApplicationContext().getPackageName(), PackageManager.GET_META_DATA);
            WXAppid = mApplicationInfo.metaData.getString("WXAPPID");
            return WXAppid;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("WXAPPID", "缺少 <meta-data android:name=\"WXAPPID\" android:value=\"your wxAppId\"/> in your AndroidManifest.xml file.");

        }
        return "";
    }
    public WxpayModule(ReactApplicationContext reactContext) {
        super(reactContext);
        rContext = reactContext;
    }

    @Override
    public String getName() {
        return "wxPay";
    }

    @ReactMethod
    public void pay(final ReadableMap order, Promise promise) {
        WxpayModule.promise = promise;
        PayReq request = new PayReq();
        request.appId = order.getString("appid");
        request.partnerId = order.getString("partnerid");
        request.prepayId= order.getString("prepayid");
        request.packageValue = order.getString("package");
        request.nonceStr= order.getString("nonce_str");
        request.timeStamp= order.getString("timestamp");
        request.sign= order.getString("sign");

        getWXApi().sendReq(request);
    }

    @ReactMethod
    public void isSupported(Promise promise) { // 判断是否支持调用微信SDK
        boolean isSupported = getWXApi().isWXAppInstalled();
        promise.resolve(isSupported);
    }

    @ReactMethod
    public void goMiniProgram( ReadableMap dic, Promise promise) { // 打开小程序

        WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
        req.userName = dic.getString("userName");       // 填小程序原始id
        req.path = dic.getString("path");               //拉起小程序页面的可带参路径，不填默认拉起小程序首页

        int pti = 0;// WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版，体验版和正式版
        try{
            pti = Integer.parseInt(dic.getString("miniProgramType"));
        }catch(NumberFormatException e){
            pti = 0;
        }
        req.miniprogramType = pti;
        boolean isSupported = getWXApi().sendReq(req);
        promise.resolve( isSupported ? "1" : "0");
    }

}
